package cn.topevery.garden.dal.dao.impl;

import cn.topevery.garden.dal.dao.GreMapStatDao;
import cn.topevery.garden.entity.oneMap.CountVO;
import cn.topevery.garden.entity.po.GreMapStat;
import cn.topevery.garden.entity.query.GreMapStatQuery;
import cn.topevery.garden.entity.vo.GreMapStatVO;
import cn.topevery.garden.framework.base.BaseDaoImpl;
import hw.topevery.framework.db.CommandType;
import hw.topevery.framework.db.DbCommand;
import hw.topevery.framework.db.entity.DataPager;
import hw.topevery.framework.db.entity.SqlQueryMate;
import hw.topevery.framework.db.enums.ScriptConditionEnum;
import hw.topevery.framework.entity.DbCsp;
import hw.topevery.framework.entity.KeyValue;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicReference;

/**
 * 
 *
 * @author code-generate
 * @date 2022-01-05
 */
@Repository
public class GreMapStatDaoImpl extends BaseDaoImpl<GreMapStat> implements GreMapStatDao {
    @Override
    public <P extends DbCsp> List<GreMapStat> getSearch(P para) {
        return null;
    }

    

    @Override
    public DataPager<GreMapStatVO> listPage(GreMapStatQuery query) {
        String table = "t_gre_map_stat tgms";

        String columns = "tgms.*";

        int pageIndex = query.pageIndex - 1;
        int pageSize = query.pageSize;

        SqlQueryMate sqlQueryMate = new SqlQueryMate(table).setColumns(columns).setOrderBy("tgms.c_id desc");
        sqlQueryMate
                .where(true, "tgms.c_db_status", ScriptConditionEnum.Eq, 0)
        ;

        return getDataPager(sqlQueryMate, pageSize, pageIndex, GreMapStatVO.class);
    }

    @Override
    public GreMapStat getEntityByColumn(String key,String value){
        KeyValue<String> map=new KeyValue<>();
        map.key=key;
        map.value=value;
        return getEntityByColumnEqValue(map);
    }

    @Override
    public void updateSql(GreMapStat greMapStat) {
        StringBuilder sql = new StringBuilder("update t_gre_map_stat set\n" +
                "        c_update_time=now(),\n" +
                "        c_area=").append(greMapStat.getArea());
        if (StringUtils.isNotBlank(greMapStat.getGeomWkt())) {
            sql.append("c_geom=ST_GeometryFromText(").append(greMapStat.getGeomWkt());
        } else {
            sql.append("c_geom=null");
        }
        sql.append(" where c_db_status=0 and c_id=").append(greMapStat.getId());
        run(sql.toString(), CommandType.Text, DbCommand::executeNonQuery);
    }

    @Override
    public void insertSql(GreMapStat greMapStat) {
        StringBuilder sql = new StringBuilder("INSERT INTO t_gre_map_stat (\n" +
                "        c_year,\n" +
                "        c_name,\n" +
                "        c_area,\n" +
                "        c_geom,\n" +
                "        c_create_time,\n" +
                "        c_db_status\n" +
                "        )\n" +
                "        VALUES\n" +
                "        (").append(greMapStat.getYear()).append(",")
                .append(greMapStat.getName()).append(",")
                .append(greMapStat.getArea()).append(",");
        if (StringUtils.isNotBlank(greMapStat.getGeomWkt())) {
            sql.append("ST_GeometryFromText(").append(greMapStat.getGeomWkt());
        } else {
            sql.append("null ,");
        }
        sql.append(greMapStat.getCreateTime()).append(",0");
        run(sql.toString(), CommandType.Text, DbCommand::executeNonQuery);
    }

    @Override
    public List<CountVO> getYears() {
        StringBuffer sb = new StringBuffer();
        sb.append("  select c_year From t_gre_map_stat group by c_year order by c_year desc\n");

        AtomicReference<List<CountVO>> vo = new AtomicReference<>(new ArrayList<>());
        run(sb.toString(), CommandType.Text, dbCommand -> {
            vo.set(dbCommand.executeToList(CountVO.class));
        });

        return vo.get();
    }

    @Override
    public List<Map<String, Object>> getAreaInfo(Integer year) {
        return getMap("select tgms.c_name `name`,tgms.c_area `value` From t_gre_map_stat tgms\n" +
                "where tgms.c_year="+year+" and tgms.c_db_status=0\n" +
                "and exists(select 1 from t_sys_class tsc\n" +
                " where tsc.c_class_pid='8c793ae3-8e14-48e7-aba6-8367cc3f0037' and tsc.c_db_status=0 and tgms.c_name=tsc.c_name )\n" +
                "order  by tgms.c_id");
    }

    @Override
    public List<GreMapStat> search(Map<String, Object> map) {
        SqlQueryMate sqlQueryMate = new SqlQueryMate("t_gre_map_stat").setColumns("*");
        sqlQueryMate.where(true, "c_db_status", ScriptConditionEnum.Eq, 0)
                .where(ObjectUtils.isNotEmpty(map.get("year")), "c_year", ScriptConditionEnum.Eq, map.get("year"));
        return selectList(sqlQueryMate, GreMapStat.class);
    }
}
